﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client;
using System.Timers;
using System.Windows.Threading;
using System.Threading;
namespace Yugi.Client.Control
{
	public partial class YScore : UserControl, IInvoke
	{
        PlayerModel model;
        System.Timers.Timer timer;
        int score;
        int newscore;
        public YScore()
		{
			this.InitializeComponent();
            timer = new System.Timers.Timer(80);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            score = 8000;
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(Health_DataContextChanged);
            k[0] = s0;
            k[1] = s1;
            k[2] = s2;
            k[3] = s3;
            k[4] = s4;
		}
        void Health_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            model = this.DataContext as PlayerModel;
            if (model == null) return;
            model.Health_Presenter = this;
        }
        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate()
            {

                if (score < newscore)
                {
                    score += 50;
                }
                if (score > newscore)
                {
                    score -= 50;
                }
                if(score==newscore)
                    timer.Enabled = false;
                SetScore(score);
            });
        }
        Image[] k=new Image[5];
        public void SetScore(int n)
        {
            for (int i = 0; i < 5; i++)
            {
                int temp = n % 10;
                n = n / 10;
                if (temp != 0 || n>0)
                {
                    string s = "/Yugi_Gui;component/Image/Particles/Number/" + temp.ToString() + ".png";
                    k[i].Source = new BitmapImage(new Uri(s, UriKind.Relative));
                }
                else
                {
                    string s = "/Yugi_Gui;component/Image/Particles/Number/-1.png";
                    k[i].Source = new BitmapImage(new Uri(s, UriKind.Relative));
                }
            }
        }
        public void RunScore(int n)
        {
            newscore = n;
            timer.Enabled = true;
        }
        #region IInvoke Members

        public void Do(ClientMSG msg, params object[] obj)
        {
            switch (msg)
            {
                case ClientMSG.DoiDiem:
                        RunScore((int)obj[1]);
                        break;
                case ClientMSG.Reset:
                    Reset();
                    break;
            }
        }

        public void Refresh()
        {

        }
        #endregion
        public void Reset()
        {
            timer.Enabled = false;
            this.score = 8000;
            SetScore(score);
            model.Reset();
        }
    }
}